﻿@model TopMenuModel
@using Nop.Web.Models.Catalog;

@helper RenderCategoryLine(CategorySimpleModel category, int level, bool responsiveMobileMenu)
{
    <li>
        <a href="@Url.RouteUrl("Category", new { SeName = category.SeName })">@category.Name
        @if (category.NumberOfProducts.HasValue)
        {
            <text> </text>@T("Categories.TotalProducts", category.NumberOfProducts.Value)
        }
        </a>
        @{
            //subcategories
            var subCategories = responsiveMobileMenu ?
                //responsive (all categories)
                category.SubCategories :
                //standard design (only categories with "IncludeInTopMenu")
                category.SubCategories.Where(x => x.IncludeInTopMenu).ToList();

            var levelClass = "";
            if (level == 0)
            {
                levelClass = "first-level";
            }
            if (subCategories.Count > 0)
            {
                <div class="sublist-toggle"></div>
                <ul class="sublist @levelClass">
                    @foreach (var subCategory in subCategories)
                    {
                        @RenderCategoryLine(subCategory, level + 1, responsiveMobileMenu)
                    }
                </ul>
            }
        }
    </li>
}

<ul class="top-menu">
    @Html.Widget("header_menu_before")
    @if (Model.DisplayHomePageMenuItem)
    {
        <li><a href="@Url.RouteUrl("HomePage")">@T("HomePage")</a></li>
    }
    @{
        var rootCategories = Model.Categories.Where(x => x.IncludeInTopMenu).ToList();
    }
    @foreach (var category in rootCategories)
    {
        @RenderCategoryLine(category, 0, false)
    }
    @foreach (var topic in Model.Topics)
    {
        <li><a href="@Url.RouteUrl("Topic", new { SeName = topic.SeName })">@topic.Name</a></li>
    }
    @if (Model.NewProductsEnabled && Model.DisplayNewProductsMenuItem)
    {
        <li><a href="@Url.RouteUrl("NewProducts")">@T("Products.NewProducts")</a></li>
    }
    @if (Model.DisplayProductSearchMenuItem)
    {
        <li><a href="@Url.RouteUrl("ProductSearch")">@T("Search")</a></li>
    }
    @if (Model.DisplayCustomerInfoMenuItem)
    {
        <li><a href="@Url.RouteUrl("CustomerInfo")">@T("Account.MyAccount")</a></li>
    }
    @if (Model.BlogEnabled && Model.DisplayBlogMenuItem)
    {
        <li><a href="@Url.RouteUrl("Blog")">@T("Blog")</a></li>
    }
    @if (Model.ForumEnabled && Model.DisplayForumsMenuItem)
    {
        <li><a href="@Url.RouteUrl("Boards")">@T("Forum.Forums")</a></li>
    }
    @if (Model.DisplayContactUsMenuItem)
    {
        <li><a href="@Url.RouteUrl("ContactUs")">@T("ContactUs")</a></li>
    }
    @Html.Widget("header_menu_after")
</ul>

@{
    var rootCategoriesResponsive = Model.Categories.ToList();
    //name it "Categories" if we have only categories. Otherwise, "Menu"
    var responsiveMenuTitle = Model.HasOnlyCategories ? T("Categories") : T("Menu");
    <div class="menu-toggle">@responsiveMenuTitle</div>
    <ul class="top-menu mobile">
        @Html.Widget("mob_header_menu_before")
        @if (Model.DisplayHomePageMenuItem)
        {
            <li><a href="@Url.RouteUrl("HomePage")">@T("HomePage")</a></li>
        }
        @foreach (var category in rootCategoriesResponsive)
        {
            @RenderCategoryLine(category, 0, true)
        }
        @foreach (var topic in Model.Topics)
        {
            <li><a href="@Url.RouteUrl("Topic", new { SeName = topic.SeName })">@topic.Name</a></li>
        }
        @if (Model.NewProductsEnabled && Model.DisplayNewProductsMenuItem)
        {
            <li><a href="@Url.RouteUrl("NewProducts")">@T("Products.NewProducts")</a></li>
        }
        @if (Model.DisplayProductSearchMenuItem)
        {
            <li><a href="@Url.RouteUrl("ProductSearch")">@T("Search")</a></li>
        }
        @if (Model.DisplayCustomerInfoMenuItem)
        {
            <li><a href="@Url.RouteUrl("CustomerInfo")">@T("Account.MyAccount")</a></li>
        }
        @if (Model.BlogEnabled && Model.DisplayBlogMenuItem)
        {
            <li><a href="@Url.RouteUrl("Blog")">@T("Blog")</a></li>
        }
        @if (Model.ForumEnabled && Model.DisplayForumsMenuItem)
        {
            <li><a href="@Url.RouteUrl("Boards")">@T("Forum.Forums")</a></li>
        }
        @if (Model.DisplayContactUsMenuItem)
        {
            <li><a href="@Url.RouteUrl("ContactUs")">@T("ContactUs")</a></li>
        }
        @Html.Widget("mob_header_menu_after")
    </ul>
    
    <script type="text/javascript">
        $(document).ready(function () {
            $('.menu-toggle').click(function () {
                $(this).siblings('.top-menu.mobile').slideToggle('slow');
            });
            $('.top-menu.mobile .sublist-toggle').click(function () {
                $(this).siblings('.sublist').slideToggle('slow');
            });
        });
    </script>
}



